/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Persistencia.Oracle;

import Persistencia.Entidades.Transporte;
import Persistencia.ITransporteDAO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import oracle.jdbc.OraclePreparedStatement;
import oracle.jdbc.OracleTypes;

/**
 *
 * @author zeta
 */
public class TransporteOracleDAO implements ITransporteDAO {

    private static TransporteOracleDAO INSTANCE = null;
    /*
     * El contrustcor private no permite que se genere un constructor por defecto
     */

    private TransporteOracleDAO() {
    }

    private synchronized static void createInstance() {
        if (INSTANCE == null) {
            INSTANCE = new TransporteOracleDAO();
        }
    }

    public static TransporteOracleDAO getInstance() {
        if (INSTANCE == null) {
            createInstance();
        }
        return INSTANCE;
    }

    public Integer insertTransporte(Transporte trans) {
       String sql = "INSERT INTO TRANSPORTE"
                + "(RAZON_SOCIAL,DIRECCION,LOCALIDAD,PROVINCIA,PAIS,"
                + "ABREVIADO,CUIT) VALUES(?,?,?,?,?,?,?) RETURNING ID_TRANSPORTE INTO ?";
        try {
            OraclePreparedStatement pst = (OraclePreparedStatement) OracleDAOFactory.getConnection().prepareStatement(sql);
            pst.setString(1, trans.getRazonSocial());
            pst.setString(2, trans.getDireccion());
            pst.setString(3, trans.getLocalidad());
            pst.setString(4, trans.getProvincia());
            pst.setString(5, trans.getPais());
            pst.setString(6, trans.getAbreviado());
            pst.setInt(7, trans.getCuit().intValue());
            pst.registerReturnParameter(8, OracleTypes.NUMBER);
            pst.executeUpdate();

            ResultSet rs = pst.getReturnResultSet();
            if (rs.next()) {
                return rs.getInt(1);
            }
            return null;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public boolean deleteTransporte(Transporte trans) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean updateTransporte(Transporte trans) {
       String sql = "UPDATE TRANSPORTE SET RAZON_SOCIAL = ?,"
                    + "DIRECCION = ?,LOCALIDAD=?,PROVINCIA=?,PAIS=?,"
                    + "ABREVIADO = ?,CUIT = ? WHERE ID_TRANSPORTE = ?";
            try {
            OraclePreparedStatement pst = (OraclePreparedStatement) OracleDAOFactory.getConnection().prepareStatement(sql);
            pst.setString(1, trans.getRazonSocial());
            pst.setString(2, trans.getDireccion());
            pst.setString(3, trans.getLocalidad());
            pst.setString(4, trans.getProvincia());
            pst.setString(5, trans.getPais());
            pst.setString(6, trans.getAbreviado());
            pst.setInt(7, trans.getCuit().intValue());
            pst.setInt(8, trans.getIdTransporte());
            return pst.execute();
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public Transporte getTransporte(Integer idTransporte) {
          String sql = "SELECT * FROM TRANSPORTE  WHERE ID_TRANSPORTE = ?";
        try {
            OraclePreparedStatement pst = (OraclePreparedStatement) OracleDAOFactory.getConnection().prepareStatement(sql);
            pst.setInt(1, idTransporte.intValue());
            ResultSet rs = pst.executeQuery();
            if(rs.next()){
                Transporte trans = new Transporte();
                trans.setIdTransporte(rs.getShort("ID_TRANSPORTE"));
                trans.setRazonSocial(rs.getString("RAZON_SOCIAL"));
                trans.setDireccion(rs.getString("DIRECCION"));
                trans.setLocalidad(rs.getString("LOCALIDAD"));
                trans.setProvincia(rs.getString("PROVINCIA"));
                trans.setPais(rs.getString("PAIS"));
                trans.setAbreviado(rs.getString("ABREVIADO"));
                trans.setCuit(rs.getLong("CUIT"));

                return trans;
            }else{
                return null;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public Transporte getTransporte(String RazonSocial) {
          String sql = "SELECT * FROM TRANSPORTE  WHERE RAZON_SOCIAL = ?";
        try {
            OraclePreparedStatement pst = (OraclePreparedStatement) OracleDAOFactory.getConnection().prepareStatement(sql);
            pst.setString(1, RazonSocial);
            ResultSet rs = pst.executeQuery();
            if(rs.next()){
                Transporte trans = new Transporte();
                trans.setIdTransporte(rs.getShort("ID_TRANSPORTE"));
                trans.setRazonSocial(rs.getString("RAZON_SOCIAL"));
                trans.setDireccion(rs.getString("DIRECCION"));
                trans.setLocalidad(rs.getString("LOCALIDAD"));
                trans.setProvincia(rs.getString("PROVINCIA"));
                trans.setPais(rs.getString("PAIS"));
                trans.setAbreviado(rs.getString("ABREVIADO"));
                trans.setCuit(rs.getLong("CUIT"));

                return trans;
            }else{
                return null;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public List<Transporte> getAllTransportes() {
       String sql = "SELECT * FROM TRANSPORTE";
        try {
            OraclePreparedStatement pst = (OraclePreparedStatement) OracleDAOFactory.getConnection().prepareStatement(sql);
               ResultSet rs = pst.executeQuery();
               List<Transporte> lTrans = new ArrayList();
            while(rs.next()){
                Transporte trans = new Transporte();
                trans.setIdTransporte(rs.getShort("ID_IMPORTADOR"));
                trans.setRazonSocial(rs.getString("RAZON_SOCIAL"));
                trans.setDireccion(rs.getString("DIRECCION"));
                trans.setLocalidad(rs.getString("LOCALIDAD"));
                trans.setProvincia(rs.getString("PROVINCIA"));
                trans.setPais(rs.getString("PAIS"));
                trans.setAbreviado(rs.getString("ABREVIADO"));
                trans.setCuit(rs.getLong("CUIT"));
                lTrans.add(trans);

            }
               return lTrans;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public List<Transporte> selectTransportes(String condicion) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
